package au.edu.swin.swinvehicle.driverlicencetype;

/**
 * DriverLicenceType entity
 * @version 1.0
 * @author Viet Bui
 * @lastmodify 2009-10-05
 */
import au.edu.swin.swinvehicle.driver.DriverEntity;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@NamedQueries({
    @NamedQuery(name = "findAllDriverLicenceTypes",
    query = "SELECT dlt FROM DriverLicenceTypeEntity dlt")
})
@Table(name = "DRIVERLICENCETYPE")
public class DriverLicenceTypeEntity implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    @Column(length = 15, unique = true, nullable = false)
    private String code;
    @Column(length = 255, nullable = false)
    private String description;
    @OneToMany(mappedBy = "driverLicenceType", cascade = CascadeType.ALL)
    private List<DriverEntity> drivers;

    /**
     * Default constructor
     */
    public DriverLicenceTypeEntity() {
        drivers = new ArrayList<DriverEntity>();
    }

    public DriverLicenceTypeEntity(String code, String description) {
        this(); // Call default constructor
        setCode(code);
        setDescription(description);
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public List<DriverEntity> getDrivers() {
        return drivers;
    }

    public void setDrivers(List<DriverEntity> drivers) {
        this.drivers = drivers;
    }

    // Transfer Object Methods
    public DriverLicenceTypeTO getData() throws DriverLicenceTypeManagerException {
        DriverLicenceTypeTO to = null;
        try {
            to = new DriverLicenceTypeTO(id, code);
        } catch (Exception e) {
            throw new DriverLicenceTypeManagerException("Cannot get DriverLicenceTypeTO data", e);
        }
        return to;
    }

    public DriverLicenceTypeDetailedTO getDetailedData() throws DriverLicenceTypeManagerException {
        DriverLicenceTypeDetailedTO to = null;

        try {
            to = new DriverLicenceTypeDetailedTO(id, code, description);
        } catch (Exception e) {
            throw new DriverLicenceTypeManagerException("Cannot get DriverLicenceTypeDetailedTO data", e);
        }
        return to;
    }

    // Only the DriverLicenceTypeDetailedTO can be used to update the state of the driver-licence-type entity
    public void setDetailedData(DriverLicenceTypeDetailedTO to) {
       
            setId(to.getId());
            setCode(to.getCode());
            setDescription(to.getDescription());
      
    }

     public void setData(DriverLicenceTypeTO to) {
       
            setId(to.getId());
            setCode(to.getCode());
       
    }

    @Override
    public String toString() {
        return "au.edu.swin.swinvehicle.driverlicencetype.DriverLicenceTypeEntity[id=" + id + "]";
    }
}
